﻿namespace HZY.Api.Admin.ApplicationServices.Systems;

/// <summary>
/// 操作日服务
/// </summary>
public class SysOperationLogService : ApplicationService<IRepository<SysOperationLog>>
{
    private readonly IRepository<SysUser> _sysUserRepository;

    public SysOperationLogService(IRepository<SysOperationLog> defaultRepository,
          IRepository<SysUser> sysUserRepository)
        : base(defaultRepository)
    {
        _sysUserRepository = sysUserRepository;
    }

    /// <summary>
    /// 获取列表数据
    /// </summary>
    /// <param name="pagingSearchInput"></param>
    /// <returns></returns>
    public async Task<PagingView> FindListAsync(PagingSearchInput<SysOperationLogSearchDto> pagingSearchInput)
    {
        var pagingView = PagingView.Create(pagingSearchInput.Page, pagingSearchInput.Size);

        var list = await _defaultRepository.Orm.Select<SysOperationLog, SysUser>()
            .LeftJoin(w => w.t1.UserId == w.t2.Id)
            .OrderByDescending(w => w.t1.CreationTime)
            .WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search.Api), w => w.t1.Api.Contains(pagingSearchInput.Search.Api))
            .WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search.Browser), w => w.t1.Browser.Contains(pagingSearchInput.Search.Browser))
            .WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search.Ip), w => w.t1.Ip.Contains(pagingSearchInput.Search.Ip))
            .WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search.OS), w => w.t1.OS.Contains(pagingSearchInput.Search.OS))
            .WhereIf(pagingSearchInput.Search.StartTime != null, w => w.t1.CreationTime.Date >= pagingSearchInput.Search.StartTime.Value)
            .WhereIf(pagingSearchInput.Search.EndTime != null, w => w.t1.CreationTime.Date <= pagingSearchInput.Search.EndTime.Value)
            .Page(pagingView)
            .ToListAsync(w => new
            {
                w.t1.Id,
                w.t1.Api,
                w.t1.Browser,
                w.t1.Ip,
                w.t1.OS,
                w.t1.TakeUpTime,
                UserName = w.t2.Name,
                w.t2.LoginName,
                w.t1.ControllerDisplayName,
                w.t1.ActionDisplayName,
                w.t1.CreationTime
            })
        ;

        return pagingView.SetDataSource(list, new Dictionary<string, Func<object?, object?>>
        {
            // 覆盖值
            ["CreationTime"] = (value) =>
            {
                if (value == null) return null;
                return ((DateTime)value).ToString("yyyy-MM-dd");
            }
        });
    }

    /// <summary>
    /// 删除所有数据
    /// </summary>
    /// <returns></returns>
    public async Task<bool> DeletedAllData()
    {
        return await _defaultRepository.DeleteAsync(w => 1 == 1) >= 0;
    }

    /// <summary>
    /// 查询表单数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<Dictionary<string, object>> FindFormAsync(Guid id)
    {
        var res = new Dictionary<string, object>();
        var form = await _defaultRepository.FindByIdAsync(id);
        form = form.NullSafe();
        var use = await _sysUserRepository.FindByIdAsync(form.UserId);
        use = use.NullSafe();
        res[nameof(id)] = id == Guid.Empty ? "" : id;
        res[nameof(form)] = form;
        res[nameof(use)] = use;
        return res;
    }
}